package com.jlnk.androidfmc.fragment.fragments;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;
import com.jlnk.androidfmc.R;
import com.jlnk.androidfmc.fragment.FollowFragment;
import com.jlnk.androidfmc.fragment.LatestFragment;
import com.jlnk.androidfmc.view.MyNyq;
import com.jlnk.androidfmc.view.NyqPost;


public class FarmerCircleFragment extends Fragment {
    private TextView textViewFollow;
    private TextView textViewLatest;
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private FragmentManager fragmentManager;
    private FollowFragment followFragment;
    private LatestFragment latestFragment;

    public FarmerCircleFragment() {
        // Required empty public constructor
    }

    public static FarmerCircleFragment newInstance(String param1, String param2) {
        FarmerCircleFragment fragment = new FarmerCircleFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        final  View view = inflater.inflate(R.layout.fragment_farmercircle, container, false);


        // 初始化控件
        textViewFollow = view.findViewById(R.id.textViewFollow);
        textViewLatest = view.findViewById(R.id.textViewLatest);

        // 获取FragmentManager
        fragmentManager = getChildFragmentManager();

        // 初始化FollowFragment和LatestFragment
        followFragment = new FollowFragment();
        latestFragment = new LatestFragment();

        // 默认显示LatestFragment
        switchFragment(latestFragment);
        // 设置点击事件
        setClickListeners(view);

        return view;
    }

    private void setClickListeners(View view) {
        textViewFollow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (fragmentManager != null && followFragment.isAdded()) {
                    fragmentManager.beginTransaction().replace(R.id.frameLayoutContent, followFragment).commit();
                }
            }
        });

        textViewLatest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (fragmentManager != null && latestFragment.isAdded()) {
                    fragmentManager.beginTransaction().replace(R.id.frameLayoutContent, latestFragment).commit();
                }
            }
        });

        // 我的按钮点击事件
        view.findViewById(R.id.btnMy).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goToMyNyqActivity();
            }
        });

        // 发布按钮点击事件
        ImageButton btnRelease = view.findViewById(R.id.btnRelease);
        if (btnRelease != null) {
            btnRelease.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // 确保当前Fragment已经附加到Activity
                    if (isAdded()) {
                        goToNyqPostActivity();
                    }
                }
            });
        } else {
            Log.e("FarmerCircleFragment", "ImageButton not found");
        }
    }

    private void switchFragment(Fragment fragment) {
        if (fragment != null && fragmentManager != null) {
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            transaction.replace(R.id.frameLayoutContent, fragment);
            transaction.commit(); // 确保调用了commit()
        }
    }



    private void goToMyNyqActivity() {
        // 使用 requireContext 来确保 Fragment 已经附加到 Activity
        Intent intent = new Intent(requireContext(), MyNyq.class);
        startActivity(intent);
    }

    private void goToNyqPostActivity() {
        Intent intent = new Intent(requireActivity(), NyqPost.class);
        Log.d("FarmerCircleFragment", "Starting NyqPost Activity");
        startActivity(intent);
    }






}